package cn.iocoder.yudao.module.common.controller.admin.knowledgepoint;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.knowledgepoint.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.knowledgepoint.KnowledgePointDO;
import cn.iocoder.yudao.module.common.service.knowledgepoint.KnowledgePointService;

@Tag(name = "管理后台 - 知识点")
@RestController
@RequestMapping("/common/knowledge-point")
@Validated
public class KnowledgePointController {

    @Resource
    private KnowledgePointService knowledgePointService;

    @PostMapping("/create")
    @Operation(summary = "创建知识点")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point:create')")
    public CommonResult<Long> createKnowledgePoint(@Valid @RequestBody KnowledgePointSaveReqVO createReqVO) {
        return success(knowledgePointService.createKnowledgePoint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新知识点")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point:update')")
    public CommonResult<Boolean> updateKnowledgePoint(@Valid @RequestBody KnowledgePointSaveReqVO updateReqVO) {
        knowledgePointService.updateKnowledgePoint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除知识点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:knowledge-point:delete')")
    public CommonResult<Boolean> deleteKnowledgePoint(@RequestParam("id") Long id) {
        knowledgePointService.deleteKnowledgePoint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得知识点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point:query')")
    public CommonResult<KnowledgePointRespVO> getKnowledgePoint(@RequestParam("id") Long id) {
        KnowledgePointDO knowledgePoint = knowledgePointService.getKnowledgePoint(id);
        return success(BeanUtils.toBean(knowledgePoint, KnowledgePointRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得知识点分页")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point:query')")
    public CommonResult<PageResult<KnowledgePointRespVO>> getKnowledgePointPage(@Valid KnowledgePointPageReqVO pageReqVO) {
        PageResult<KnowledgePointDO> pageResult = knowledgePointService.getKnowledgePointPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KnowledgePointRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出知识点 Excel")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point:export')")
    @OperateLog(type = EXPORT)
    public void exportKnowledgePointExcel(@Valid KnowledgePointPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KnowledgePointDO> list = knowledgePointService.getKnowledgePointPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "知识点.xls", "数据", KnowledgePointRespVO.class,
                        BeanUtils.toBean(list, KnowledgePointRespVO.class));
    }

}